package com.maserb.monitor.repository.impl;

import com.maserb.monitor.common.IdUtils;
import com.maserb.monitor.common.TreeItemModelFactory;
import com.maserb.monitor.database.JdbcTemplateFactory;
import com.maserb.monitor.database.PreparedStatementUtils;
import com.maserb.monitor.entity.DeviceTreeEntity;
import com.maserb.monitor.entity.DeviceTreeEntity;
import com.maserb.monitor.model.TreeItemModel;
import com.maserb.monitor.repository.DeviceRepository;
import com.maserb.monitor.repository.DeviceTreeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Repository;
import org.thymeleaf.util.StringUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Repository
public class DeviceTreeRepositoryImpl implements DeviceTreeRepository {
    @Autowired
    JdbcTemplateFactory jdbcTemplateFactory;

    @Override
    public void save(DeviceTreeEntity entity, String deviceCode) {
        try {
            JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(deviceCode);

            String querySql = "select count(*) from device_tree where id = ?";
            Integer count = jdbcTemplate.queryForObject(querySql, new Object[]{entity.getId()}, Integer.class);
            if (count > 0) {
                String updateSql = "update device_tree d set d.name = ?,d.code = ?,d.parentId = ?,d.nodeType = ?,d.createTime = ?,d.description = ? where d.id = ?";
                prepareSaveParameters(entity, jdbcTemplate, updateSql);
            } else {
                String insertSql = "insert into device_tree(name,code,parentId,nodeType,createTime,description,id) values(?,?,?,?,?,?,?)";
                prepareSaveParameters(entity, jdbcTemplate, insertSql);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void loadChildren(TreeItemModel item, String deviceCode) {
        if (!StringUtils.isEmpty(deviceCode)) {
            JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(deviceCode);
            if (jdbcTemplate != null) {
                String findSql = "select * from device_tree";
                List<DeviceTreeEntity> ret = jdbcTemplate.query(findSql, new Object[]{}, new BeanPropertyRowMapper<DeviceTreeEntity>(DeviceTreeEntity.class));
                if (ret != null && ret.size() > 0) {
                    loadMyChidren(item, ret);
                }
            }
        }
    }

    @Override
    public void deleteById(String key, String deviceCode) {
        if (!StringUtils.isEmpty(deviceCode)) {
            JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(deviceCode);
            if (jdbcTemplate != null) {
                String deleteSql = "delete from device_tree where id = ?";
                jdbcTemplate.update(deleteSql, key);
            }
        }
    }

    public void loadMyChidren(TreeItemModel item, List<DeviceTreeEntity> list) {
        for (DeviceTreeEntity node : list) {
            if (node.getParentId().equals(item.getId())) {
                TreeItemModel itemModel = TreeItemModelFactory.createTreeItemModel(node);
                item.getChildren().add(itemModel);
                loadMyChidren(itemModel, list);
            }
        }
    }

    @Override
    public DeviceTreeEntity findOne(String id, String code) {
        JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(code);
        if (jdbcTemplate != null) {
            String findSql = "select * from device_tree where id=?";
            List<DeviceTreeEntity> ret = jdbcTemplate.query(findSql, new Object[]{id}, new BeanPropertyRowMapper<DeviceTreeEntity>(DeviceTreeEntity.class));
            if (ret != null && ret.size() > 0) {
                return ret.get(0);
            }
        }
        return null;
    }

    private void prepareSaveParameters(DeviceTreeEntity entity, JdbcTemplate jdbcTemplate, String updateSql) {
        jdbcTemplate.update(updateSql, ps -> {
            PreparedStatementUtils.setString(ps, 1, entity.getName());
            PreparedStatementUtils.setString(ps, 2, entity.getCode());
            PreparedStatementUtils.setString(ps, 3, entity.getParentId());
            PreparedStatementUtils.setInt(ps, 4, entity.getNodeType());
            PreparedStatementUtils.setString(ps, 5, entity.getCreateTime());
            PreparedStatementUtils.setString(ps, 6, entity.getDescription());
            PreparedStatementUtils.setString(ps, 7, entity.getId());
        });
    }
}
